package com.primal.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.util.MimeType;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author:whh
 * @date: 2024-01-30 20:42
 * <p>freemarker配置</p>
 */
@Configuration
public class FreeMarkerConfig {
    public static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");

    public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
    public static final String DEFAULT_PREFIX = "";
    private Map<String, String> settings = new HashMap<>();
    public static final String DEFAULT_SUFFIX = ".html";
    public static final boolean CACHE = false;

    public static final String requestContextAttribute = "request";

    /**
     * Whether all request attributes should be added to the model prior to merging with
     * the template.
     */
    public static final boolean exposeRequestAttributes = true;

    /**
     * Whether all HttpSession attributes should be added to the model prior to merging
     * with the template.
     */
    public static final boolean exposeSessionAttributes = false;

    /**
     * Whether HttpServletRequest attributes are allowed to override (hide) controller
     * generated model attributes of the same name.
     */
    public static final boolean allowRequestOverride = false;

    /**
     * Whether to expose a RequestContext for use by Spring's macro library, under the
     * name "springMacroRequestContext".
     */
    public static final boolean exposeSpringMacroHelpers = true;

    /**
     * Whether HttpSession attributes are allowed to override (hide) controller generated
     * model attributes of the same name.
     */
    public static final boolean allowSessionOverride = false;


    @Bean
    FreeMarkerConfigurer freeMarkerConfigurer() {
        FreeMarkerConfigurer factory = new FreeMarkerConfigurer();
        factory.setTemplateLoaderPaths(DEFAULT_TEMPLATE_LOADER_PATH);
        factory.setPreferFileSystemAccess(false);
        factory.setDefaultEncoding(DEFAULT_CHARSET.name());
        Properties settings = new Properties();
        settings.put("recognize_standard_file_extensions", "true");
        settings.putAll(settings);
        factory.setFreemarkerSettings(settings);
        return factory;
    }


    /**
     * DispatcherServlet#initStrategies方法初始化时会加载该ViewResolver
     * @return
     */
    @Bean
    FreeMarkerViewResolver freeMarkerViewResolver() {
        FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
        resolver.setPrefix(DEFAULT_PREFIX);
        resolver.setSuffix(DEFAULT_SUFFIX);
        resolver.setCache(CACHE);
        resolver.setContentType(DEFAULT_CONTENT_TYPE.toString());
        resolver.setExposeRequestAttributes(exposeRequestAttributes);
        resolver.setAllowRequestOverride(allowRequestOverride);
        resolver.setAllowSessionOverride(allowSessionOverride);
        resolver.setExposeSessionAttributes(exposeSessionAttributes);
        resolver.setExposeSpringMacroHelpers(exposeSpringMacroHelpers);
        resolver.setRequestContextAttribute(requestContextAttribute);
        // The resolver usually acts as a fallback resolver (e.g. like a
        // InternalResourceViewResolver) so it needs to have low precedence
        resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
        return resolver;
    }
}
